home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MACSHELL / MS1 / COMMANDS / LISTCMDS.C < prev    next >
Text File  |  1992-12-02  |  13KB  |  517 lines

  1. /*
  2.  *    MacShell Source File
  3.  *
  4.  *    Copyright (c) 1989, 1990, 1991, 1992  Suick Bay Technologies.  All rights reserved.
  5.  *
  6.  *
  7.  *    RESTRICTIONS ON MacShell program and source code.
  8.  *
  9.  *    Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
  10.  *    restricted use by the owner of the CDROM "Disk to the future II".
  11.  *
  12.  *    Ñ╩No permission is granted for any commercial use without the written
  13.  *    consent of the Suick Bay Technologies.
  14.  *
  15.  *    Ñ╩No permission is granted for any redistribution of any kind use without
  16.  *    the written consent of the Suick Bay Technologies.
  17.  *
  18.  *    Ñ╩Permission is granted to use this for any personal noncommercial use.
  19.  *
  20.  *    Ñ╩You may not distribute source or executable code at all, nor may you 
  21.  *    distribute it with or within a commercial product without the written
  22.  *    consent of the Suick Bay Technologies.  Please send modifications to 
  23.  *    the author for inclusion in updates to the program.  Thanks.
  24.  *
  25.  *
  26.  *    MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  27.  *    WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  28.  *    PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  29.  *
  30.  *    SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  31.  *    INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
  32.  *    OR ANY PART THEREOF. 
  33.  *
  34.  *    In no event will Suick Bay Technologies be liable for any lost revenue
  35.  *    or profits or other special, indirect and consequential damages, even if
  36.  *    Suick Bay Technologies has been advised of the possibility of such damages.
  37.  *
  38.  *    Suick Bay Technologies can be reached at:
  39.  *    
  40.  *    8768 Cottonwood lane
  41.  *    Maple Grove, MN 55369
  42.  *    Voice: (612) 425-7025
  43.  *    AppleLink: D5233
  44.  *    
  45.  *
  46.  *    No parts of this software may be reproduced or stored in a
  47.  *    retrieval system or transmitted in any form, or any means,
  48.  *    electronic, mechanical, photocopying, recording or otherwise,
  49.  *    without the prior written permission of Suick Bay Technologies.
  50.  *    
  51.  *    Spread the word and not the disk.
  52.  *    
  53.  *    SPK 040160    :    reversed -m and -l options
  54.  *    SPK 030190    :    Update for Mac, DOS paths
  55.  *    SPK 012290    :    Initial
  56.  */
  57.  
  58. #include    "SystemPub.h"
  59. #include    "Proc.h"
  60. #include    "ShellPub.h"
  61. #include    "Path.h"
  62. #include    "Prefs.h"
  63.  
  64. #define        lslmode        (**MyShell).Proc[ProcID].bflags.f0
  65. #define        lsdmode        (**MyShell).Proc[ProcID].bflags.f1
  66. #define        lscmode        (**MyShell).Proc[ProcID].bflags.f2
  67. #define        lstmode        (**MyShell).Proc[ProcID].bflags.f3
  68. #define        dirSpec        (**MyShell).Proc[ProcID].bflags.f4
  69. #define        lsHead        (**MyShell).Proc[ProcID].bflags.f5
  70. #define        lsamode        (**MyShell).Proc[ProcID].bflags.f6
  71. #define        lsmmode        (**MyShell).Proc[ProcID].bflags.f6
  72.  
  73. /*******************************************************************/
  74.  
  75. #define        fLocked        1
  76. #define        fSystem        0x1000
  77. #define        fInited        0x0100
  78. #define        fShared        0x0040
  79. #define        fSwtLan        0x0020
  80. #define        fNoInit        0x0080
  81. #define        fBusy        0x0400
  82. #define        fChange        0x0200
  83.  
  84. GetFileAttrString( HFileInfo *pb, char *str )
  85. {
  86. int16        forceAttr,
  87.             attrLocked,
  88.             attrInvis,
  89.             attrBundle,
  90.             attrSystem,
  91.             attrInited,
  92.             attrDeskTop,
  93.             attrShared,
  94.             attrSwitch;
  95.  
  96.  
  97.     attrLocked    =    (pb->ioFlAttrib & fLocked);
  98.     attrInvis    =    (pb->ioFlFndrInfo.fdFlags & fInvisible);
  99.     attrBundle    =    (pb->ioFlFndrInfo.fdFlags & fHasBundle);
  100.     attrSystem    =    (pb->ioFlFndrInfo.fdFlags & fSystem);
  101.     attrInited    =    (pb->ioFlFndrInfo.fdFlags & fInited);
  102.     attrDeskTop    =    (pb->ioFlFndrInfo.fdFlags & fOnDesk);
  103.     attrShared    =    (pb->ioFlFndrInfo.fdFlags & fShared);
  104.     attrSwitch    =    (pb->ioFlFndrInfo.fdFlags & fSwtLan);
  105.     
  106.     str[ 0 ] = ( attrLocked ? 'L' : 'l' );
  107.     str[ 1 ] = ( attrInvis     ? 'V' : 'v' );
  108.     str[ 2 ] = ( attrBundle    ? 'B' : 'b' );
  109.     str[ 3 ] = ( attrSystem    ? 'S' : 's' );
  110.     str[ 4 ] = ( attrInited    ? 'I' : 'i' );
  111.     str[ 5 ] = ( attrDeskTop? 'D' : 'd' );
  112.     str[ 6 ] = ( attrShared    ? 'M' : 'm' );
  113.     str[ 7 ] = ( attrSwitch    ? 'A' : 'a' );
  114.     str[ 8 ] = '\0';
  115. }
  116.  
  117. /*******************************************************************/
  118.  
  119. void    LsHeader( WHandle ShellWh, int16 ProcID )
  120. {
  121. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  122.  
  123.     if( lslmode && lsHead )
  124.         {
  125.         lsHead = FALSE;
  126.         
  127.         procPrintf( ShellWh, ProcID,
  128. "Name                  Type Crtr  Size    Flags    Last-Mod-Date     Creation-Date\n" );
  129.         procPrintf( ShellWh, ProcID,
  130. "--------------------  ---- ---- ------ -------- ----------------- -----------------\n" );
  131.         }
  132.     else if( lsmmode && lsHead )
  133.         {
  134.         lsHead = FALSE;
  135.         
  136.         procPrintf( ShellWh, ProcID,
  137.             "  Flags  Type Crtr  Size   Name\n" );
  138.         procPrintf( ShellWh, ProcID,
  139.             "-------- ---- ---- ------ --------------------\n" );
  140.         }
  141. }
  142.  
  143. /*******************************************************************/
  144.  
  145. void    ListCallBack( WHandle ShellWh, int16 ProcID, char *path, char *last,
  146.                         pathType what, int16 vRefNum, int32 dirID )
  147. {
  148. char            str[ 256 ], dirChar;
  149. char            modified[ 64 ], created[ 64 ];
  150. OSErr            err;
  151. DateTimeRec        modDate, creDate;
  152. int32            fLen;
  153. char            type[ 16 ], crea[ 16 ], flags[ 16 ], *dir, scale = ' ';
  154. CInfoPBRec         pb;
  155. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  156.  
  157.     LsHeader( ShellWh, ProcID );
  158.     
  159.     strcpy( str, last );
  160.     CtoPstr( str );
  161.     
  162.     dirChar = DirSep();
  163.     
  164.     if( what == pathIsDir )
  165.         {
  166.         pb.dirInfo.ioCompletion     = NULL;
  167.         pb.dirInfo.ioNamePtr         = NULL;
  168.         pb.dirInfo.ioVRefNum         = vRefNum;
  169.         pb.dirInfo.ioDrDirID             = dirID;
  170.         pb.dirInfo.ioFDirIndex         = -1;
  171.         
  172.         err = PBGetCatInfo( &pb, FALSE );
  173.  
  174.         fLen = 0L;
  175.         if( lslmode || lsmmode )
  176.             GetDirSize( str, vRefNum, dirID, &fLen );
  177.  
  178.         Secs2Date( pb.dirInfo.ioDrCrDat, &creDate );
  179.         Secs2Date( pb.dirInfo.ioDrMdDat, &modDate );
  180.  
  181.         GetFileAttrString( (HFileInfo *)  &pb, flags );
  182.         strcpy( type, "Fldr" );
  183.         strcpy( crea, "Fldr" );
  184.         }
  185.     else if( what == pathIsVol )
  186.         {
  187.         unsigned int32        blksize, total;
  188.         HVolumeParam        vpb;
  189.         fLen = 0L;
  190.  
  191.         vpb.ioCompletion = 0L; 
  192.         vpb.ioNamePtr    = (StringPtr) str;
  193.         vpb.ioVRefNum     = vRefNum;
  194.         vpb.ioVolIndex    = (-1);
  195.  
  196.         err = PBHGetVInfo( &vpb, FALSE );
  197.         blksize  = (unsigned int32) vpb.ioVAlBlkSiz;
  198.  
  199.         total = (unsigned int32) vpb.ioVNmAlBlks & 0x0000FFFF;
  200.         total *= blksize;
  201.  
  202.         fLen = (unsigned int32) total;
  203.  
  204.         Secs2Date( vpb.ioVCrDate, &creDate );
  205.         Secs2Date( vpb.ioVLsMod, &modDate );
  206.  
  207.         GetFileAttrString( (HFileInfo *) &vpb, flags );
  208.         strcpy( type, "Volm" );
  209.         strcpy( crea, "Volm" );
  210.         }
  211.     else
  212.         {
  213.         pb.hFileInfo.ioCompletion     = NULL;
  214.         pb.hFileInfo.ioNamePtr         = (StringPtr) str;
  215.         pb.hFileInfo.ioVRefNum         = vRefNum;
  216.         pb.hFileInfo.ioDirID         = dirID;
  217.         pb.hFileInfo.ioFDirIndex     = 0;
  218.         
  219.         err = PBHGetFInfo( &pb, FALSE );
  220.         
  221.         fLen = pb.hFileInfo.ioFlLgLen + pb.hFileInfo.ioFlRLgLen;
  222.  
  223.         GetFileAttrString( (HFileInfo *)  &pb, flags );
  224.  
  225.         Secs2Date( pb.hFileInfo.ioFlCrDat, &creDate );
  226.         Secs2Date( pb.hFileInfo.ioFlMdDat, &modDate );
  227.  
  228.         TypeToStr( pb.hFileInfo.ioFlFndrInfo.fdType, type );
  229.         TypeToStr( pb.hFileInfo.ioFlFndrInfo.fdCreator, crea );
  230.         }
  231.                 
  232.  
  233.     if( dirSpec )
  234.         sprintf( str, ":%s", last );
  235.     else if( what == pathIsFile )
  236.         sprintf( str, "%s:%s", path, last );
  237.     else
  238.         strcpy( str, path );
  239.  
  240.     MacToPath( str );
  241.     if( lslmode || lsmmode )
  242.         {
  243.         int16        len = strlen( str );
  244.         Boolean    kf = FALSE;
  245.         
  246.         if( fLen > 32000L )
  247.             {
  248.             fLen /= 1024L;
  249.             len = (int16) fLen;
  250.             scale = 'K';
  251.             
  252.             if( fLen > 32000L )
  253.                 {
  254.                 fLen /= 1024L;
  255.                 len = (int16) fLen;
  256.                 scale = 'M';
  257.                 }
  258.             }
  259.         else
  260.             len = (int16) fLen;
  261.         
  262.         if( lsmmode )
  263.             {
  264.             if( lsdmode && (what == pathIsDir) || !lsdmode )
  265.                 procPrintf( ShellWh, ProcID,  "%8s %4s %4s %5d%c %s\n",
  266.                  flags, type, crea, len, scale,  str );
  267.             }
  268.         else
  269.             {
  270.             if( len > 21 )
  271.                 {
  272.                 str[ 21 ] = '\0';
  273.                 str[ 20 ] = '╔';    
  274.                 }
  275.         
  276.             sprintf( modified, "%02.0d/%02.0d/%2.0d %02.0d:%02.0d %s" ,modDate.month,
  277.                 modDate.day,modDate.year-1900, modDate.hour % 12, modDate.minute,
  278.                 (  modDate.hour < 12 ? "AM" : "PM") );
  279.     
  280.             if( creDate.year > 2000 )
  281.                 creDate.year -= 100;
  282.     
  283.             sprintf( created, "%02.0d/%02.0d/%2.0d %02.0d:%02.0d %s" ,creDate.month,
  284.                 creDate.day, creDate.year-1900, creDate.hour % 12, creDate.minute,
  285.                 ( creDate.hour < 12 ? "AM" : "PM") );
  286.     
  287.             if( lsdmode && (what == pathIsDir) || !lsdmode )
  288.                 procPrintf( ShellWh, ProcID,  "%-22s%4s %4s %5d%c %8s %17s %17s\n",
  289.                  str, type, crea, len, scale, flags, modified, created );
  290.             }
  291.         }
  292.     else    /* plain mode */
  293.         if( lsdmode && (what == pathIsDir) || !lsdmode )
  294.             procPrintf( ShellWh, ProcID,  "%s%c\n", str,
  295.                 ((what == pathIsDir) ? dirChar : ' ') );
  296. }
  297.  
  298. /*******************************************************************/
  299.  
  300. void    ListNames( WHandle ShellWh, int16 ProcID, char *argument )
  301. {
  302. ShellWindRec    **MyShell;
  303. char            buf[ 256 ];
  304. pathType        pt;
  305.  
  306.     MyShell = (ShellWindRec **) (**ShellWh).thing;
  307.  
  308.     lsHead = TRUE;
  309. /*
  310.  *    if the argument is a directory then we must list it
  311.  */
  312.       strcpy( buf, argument );
  313.          
  314.      if( strcmp( argument, "//" ) == 0 )
  315.           strcat( buf, "*" );
  316.  
  317.     pt =  SetCurrPath( buf );
  318.     
  319. /*     strcpy( buf, argument );
  320. */
  321.     if( (pt == pathIsDir) || (pt == pathIsVol) )
  322.         {
  323.         char    *cp;
  324.         
  325.         dirSpec = TRUE;
  326.         cp = buf;
  327.         while( *cp ) cp++;
  328.         cp--;
  329.         
  330.         if( ShellPrefs.useMacOSPath )
  331.             {
  332. /*
  333.             if( (pt == pathIsDir) && (*buf != ':' ) )
  334.                 {
  335.                 strcpy( buf, ":" );            
  336.                 strcat( buf, argument );
  337.                 cp = buf;
  338.                 while( *cp ) cp++;
  339.                 cp--;
  340.                 }
  341. */
  342.             if( *cp == '*' )
  343.                 ;
  344.             else if( *cp == ':' )
  345.                 strcat( buf, "*" );
  346.             else
  347.                 strcat( buf, ":*" );
  348.             }
  349.         else if( ShellPrefs.useUNIXPath )
  350.             {
  351.             if( *cp == '*' )
  352.                 ;
  353.             else if( *cp == '/' )
  354.                 strcat( buf, "*" );
  355.             else
  356.                 strcat( buf, "/*" );
  357.             }
  358.         else if( ShellPrefs.useDOSPath )
  359.             {
  360.             if( *cp == '*' )
  361.                 ;
  362.             else if( *cp == '\\' )
  363.                 strcat( buf, "*" );
  364.             else
  365.                 strcat( buf, "\\*" );
  366.             }
  367.         }
  368.     else
  369.         dirSpec = FALSE;
  370.  
  371.     ResetShellPWD( ShellWh );
  372.     
  373.     if( lsamode )
  374.         {
  375.  
  376.         }
  377.             
  378.     ExpandPath( ShellWh, ProcID, buf,(ProcPtr)  ListCallBack,
  379.             (**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
  380. }
  381.  
  382. /*******************************************************************/
  383.  
  384. Boolean        DoLS( int16 ProcToken, WHandle ShellWh, int16 ProcID, char *string )
  385. {
  386. char            *cp, argument[ 256 ];
  387. int16            i, err, argc;
  388. OsType            t;
  389. ShellWindRec    **MyShell;
  390.     
  391.     MyShell = (ShellWindRec **) (**ShellWh).thing;
  392.  
  393.     switch( ProcToken )
  394.         {
  395.         case    PROC_INIT    :
  396.             (**MyShell).Proc[ ProcID ].flags = TRUE;
  397.             break;
  398.                 
  399.         case    PROC_TERM    :
  400.         case    PROC_BREAK    :
  401.             /* Tell the shell that we're done */
  402.             SendOutToken( ShellWh, ProcID, PROC_BREAK );
  403.             /* Turn ourself off */
  404.             (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  405.             break;
  406.             
  407.         case    PROC_STDIN    :
  408.             if( (**MyShell).Proc[ ProcID ].flags )
  409.                 {
  410.                 Boolean        hadDirs = FALSE;
  411.                 (**MyShell).Proc[ ProcID ].flags = FALSE;        
  412.                 lslmode = FALSE;
  413.                 lsdmode = FALSE;
  414.                 lsamode = FALSE;
  415.                 lsmmode = FALSE;
  416.                 ScanInit();
  417.                 argc = (**MyShell).Proc[ ProcID ].argc;
  418.                 
  419.                 /* get arguments */
  420.                 for( i = 1; i < argc; i++ )
  421.                     {
  422.                     GetArgv( ShellWh, ProcID, i, argument );
  423.                     cp = argument;
  424.                     
  425.                     if( *cp++ == '-' )
  426.                         {
  427.                         while( *cp )
  428.                             switch( *cp++ )
  429.                                 {
  430.                                 case    'l'    :        /* Mac long format */
  431.                                     lsmmode = TRUE;
  432.                                     break;
  433.  
  434.                                 case    'm'    :        /* long listing */
  435.                                     lslmode = TRUE;
  436.                                     break;
  437.                             
  438.                                 case    'd'    :        /* list only directories */
  439.                                     lsdmode = TRUE;
  440.                                     break;
  441.  
  442.                                 case    '1'    :        /* one per line */
  443.                                     break;
  444.  
  445.                                 case    'a'    :        /* all entries incl, those that start with . */
  446.                                     lsamode = TRUE;
  447.                                     break;
  448.  
  449.                                 case    't'    :        /* Type */
  450.                                     i++;
  451.                                     if( i < argc )
  452.                                         {
  453.                                         char    temp[ 256 ];
  454.                                         
  455.                                         GetArgv( ShellWh, ProcID, i, temp );
  456.                                         t = StrToType( temp );
  457.                                         ScanForTYPE( t );
  458.                                         }
  459.                                     break;
  460.                                     
  461.                                 case    'c'    :        /* Creator */
  462.                                     i++;
  463.                                     if( i < argc )
  464.                                         {
  465.                                         char    temp[ 256 ];
  466.                                         
  467.                                         GetArgv( ShellWh, ProcID, i, temp );
  468.                                         t = StrToType( temp );
  469.                                         ScanForCREA( t );
  470.                                         }
  471.                                     break;
  472.                                 }
  473.                         }
  474.                     }
  475.  
  476.                 /* do list dirs */    
  477.                 for( i = 1; i < (**MyShell).Proc[ ProcID ].argc; i++ )
  478.                     {
  479.                     GetArgv( ShellWh, ProcID, i, argument );
  480.                     if( *argument != '-' )
  481.                         {
  482.                         hadDirs = TRUE;
  483.                         ListNames( ShellWh, ProcID, argument );
  484.                         }
  485.                     else    /* look for type and creator fields */
  486.                         {
  487.                         cp = argument;
  488.                         cp++;
  489.                         if( *cp == 't' || *cp == 'c' )
  490.                             i++;
  491.                         }
  492.                     }
  493.                     
  494.                 if( hadDirs == FALSE )
  495.                     {
  496.                     if( ShellPrefs.useMacOSPath )
  497.                         ListNames( ShellWh, ProcID, ":*" );
  498.                     else if( ShellPrefs.useUNIXPath )
  499.                         ListNames( ShellWh, ProcID, "*" );
  500.                     else if( ShellPrefs.useDOSPath )
  501.                         ListNames( ShellWh, ProcID, "*" );
  502.  
  503.                     }
  504.                                         
  505.                 ScanInit();
  506.  
  507.                 /* Tell the shell that we're done */
  508.                 SendOutToken(  ShellWh, ProcID, PROC_BREAK );
  509.                 
  510.                 /* Turn ourself off */
  511.                 (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  512.                 return( FALSE );
  513.                 }
  514.         }
  515. }
  516.  
  517.